<template>
	<div class="div-user-sum">
		<div class="div-top">
			<span>角色管理</span>
		</div>
		<el-button type="primary" style="margin:0 0 10px 10px" @click="roleSaveDialog=true" icon="el-icon-circle-plus-outline">添加角色</el-button>
		<div style="margin: 10px;">
			<el-table :data="tableRole" border>
				<el-table-column prop="roleName" label="角色名称" align="center">
				</el-table-column>
				<el-table-column prop="roleNo" label="角色标识" align="center">
				</el-table-column>
				<el-table-column prop="remark" label="描述" align="center">
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button type="primary" @click="openEdit(scope.row)" size="small">编辑</el-button>
						<el-button type="danger" @click="removeRole(scope.row)" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div style="width: 100%;margin: 15px 0;text-align: center;" v-show="tableRole && tableRole.length>0">
				<el-pagination background layout="total" :total="tableRole.length">
				</el-pagination>
			</div>
		</div>
		<!-- 添加角色 -->
		<el-dialog title="添加角色" :close-on-press-escape='false' :close-on-click-modal='false' :append-to-body='true' :visible.sync="roleSaveDialog" width="380px">
			<el-form :model="addForm" :rules="rules" ref="addForm">
				<el-form-item label="角色名称" prop="roleName" label-width="90px">
					<el-input v-model="addForm.roleName" placeholder='请输入角色名称' maxlength='50' clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="角色标识" prop="roleNo" label-width="90px">
					<el-input v-model="addForm.roleNo" placeholder='请输入角色标识' maxlength='50' clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="角色描述" prop="remark" label-width="90px">
					<el-input v-model="addForm.remark" placeholder='请输入角色描述' maxlength='200' clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="角色权限" prop="permissionList" label-width="90px">
					<div class="eltreeBox">
						<el-tree :data="menuData" node-key='id' :props="props" show-checkbox ref='addTree' @check-change="roleSaveChange">
						</el-tree>
					</div>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="resetAdd">取 消</el-button>
				<el-button type="primary" @click="roleSave" :disabled='dataloading'>确 定</el-button>
			</div>
		</el-dialog>
		<!-- 编辑角色窗口 -->
		<el-dialog title="编辑角色" :close-on-press-escape='false' :close-on-click-modal='false' :append-to-body='true' :visible.sync="editRoleDialog" width="380px" @opened='editOpend'>
			<el-form :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="角色标识" prop="roleNo" label-width="90px">
					<el-input v-model="editForm.roleNo" placeholder='请输入角色标识' maxlength='50' readonly clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="角色名称" prop="roleName" label-width="90px">
					<el-input v-model="editForm.roleName" placeholder='请输入角色名称' maxlength='50' clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="角色描述" prop="remark" label-width="90px">
					<el-input v-model="editForm.remark" placeholder='请输入角色描述' maxlength='200' clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="角色权限" prop="permissionList" label-width="90px">
					<div class="eltreeBox">
						<el-tree :default-expand-all='true' :data="menuData" :check-strictly='checkStrictly' node-key='id' :props="props" show-checkbox ref='editTree' @check-change="editRoleChange">
						</el-tree>
					</div>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="resetEdit">取 消</el-button>
				<el-button type="primary" @click="editRole">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	import { roleList, roleSave, roleUpdate, roleRemove } from '@/api/role.js'
	import { permissionTree, permissionSave, permissionUpdate, permissionRemove, permissionByRid } from '@/api/permission.js'
	export default {
		data() {
			return {
				menuData: [],
				schools: [],
				tableRole: [],
				dataloading: false,
				roleSaveDialog: false,
				editRoleDialog: false,
				checkStrictly: true,
				multipleSelect: [],
				props: {
					label: 'name',
					value: 'id',
				},
				addForm: {
					roleName: '',
					roleNo: '',
					remark: '',
					permissionList: [],
				},
				editForm: {
					roleId: '',
					roleName: '',
					roleNo: '',
					remark: '',
					permissionList: [],
				},
				rules: {
					roleName: [{
						required: true,
						message: '请输入角色名称'
					}],
					roleNo: [{
						required: true,
						message: '请输入角色标识'
					}],
					remark: [{
						required: true,
						message: '请输入角色描述'
					}],
				}
			}
		},
		created() {
			this.getRoleList();
			this.getRouterList();
		},
		methods: {
			getRouterList() {
				permissionTree({}).then(async res => {
					this.menuData = res.data
				})
			},
			getRoleList() {
				roleList({}).then(async res => {
					this.tableRole = res.data;
				});
			},
			roleSaveChange() {
				var checkedNodes = this.$refs.addTree.getCheckedNodes(false, true);
				var checkedKeys = [];
				checkedNodes.map(item => {
					checkedKeys.push(item.id)
				})
				this.addForm.permissionList = checkedKeys;
			},
			roleSave() {
				this.dataloading = true;
				setTimeout(_ => {
					this.dataloading = false;
				}, 1000)
				this.$refs.addForm.validate((valid) => {
					if(valid) {
						var data = Object.assign({}, this.addForm);
						roleSave(data).then(async res => {
							if(res.code == '200') {
								this.$message({
									message: '添加成功',
									type: 'success'
								});
								this.resetAdd();
								this.getRoleList();
							} else {
								this.$message({
									message: res.message,
									type: 'error'
								});
							}
						})
					}
				})
			},
			resetAdd() {
				this.$refs.addTree.setCheckedNodes([]);
				this.$refs.addForm.resetFields();
				this.$refs.addForm.clearValidate()
				this.roleSaveDialog = false;
			},
			openEdit(item) {
				permissionByRid({
					roleId: item.roleId
				}).then(res => {
					this.editForm = Object.assign({}, item, {
						permissionList: res.data || []
					});
					this.editRoleDialog = true;
					this.checkStrictly = true;
				})
			},
			editOpend() {
				this.$nextTick(() => {
					if(this.editForm.permissionList) {
						this.$refs.editTree.setCheckedKeys(this.editForm.permissionList);
					}
					this.checkStrictly = false;
				})
			},
			editRoleChange() {
				var checkedNodes = this.$refs.editTree.getCheckedNodes(false, true);
				var checkedKeys = [];
				checkedNodes.map(item => {
					checkedKeys.push(item.id)
				})
				this.editForm.permissionList = checkedKeys;
			},
			editRole() {
				this.$refs.editForm.validate((valid) => {
					if(valid) {
						var data = Object.assign({}, this.editForm);
						roleUpdate(data).then(async res => {
							if(res.code == '200') {
								this.$message({
									message: '编辑成功',
									type: 'success'
								});
								this.resetEdit();
								this.getRoleList();
							} else {
								this.$message({
									message: res.message,
									type: 'error'
								});
							}
						})
					}
				})
			},
			resetEdit() {
				this.editForm.permissionList = [];
				this.editForm.roleId = '';
				this.editForm.roleName = '';
				this.editForm.roleNo = '';
				this.editForm.remark = '';
				this.$refs.editForm.clearValidate()
				this.$refs.editTree.setCheckedNodes([]);
				this.editRoleDialog = false;
			},
			removeRole(item) {
				this.$confirm('是否删除该角色?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					roleRemove({
						roleId: item.roleId
					}).then(async res => {
						if(res.code == '200') {
							this.$message({
								message: '删除成功',
								type: 'success',
							});
						} else {
							this.$message({
								message: res.message,
								type: 'error',
							});
						}
						this.getRoleList();
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		}
	}
</script>